package com.hspedu.seckill.rabbitmq;

import cn.hutool.json.JSONUtil;
import com.hspedu.seckill.pojo.SeckillMessage;
import com.hspedu.seckill.pojo.User;
import com.hspedu.seckill.service.OrderService;
import com.hspedu.seckill.vo.GoodsVo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息接收者消费者
 */
@Component
public class MQReceiverMessage {



    @Resource
    private OrderService orderService;

    //接收seckillQueue消息队列的json字符串完成异步秒杀
    @RabbitListener(queues = "seckillQueue")
    public void receiver(String message){
        //将jsong字符串转成seckillMessage
        SeckillMessage seckillMessage = JSONUtil.toBean(message,SeckillMessage.class);
        //取出seckillMessage中的user、goodsVo
        User user = seckillMessage.getUser();
        GoodsVo goodsVo = seckillMessage.getGoodsVo();

        //完成异步秒杀
        orderService.seckill(user,goodsVo);
    }

}
